/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.LoaiSanPhamPOJO;
import POJO.SanPhamPOJO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import ultil.MySqlDataAccessHelper;

/**
 *
 * @author NGUYEN CONG BINH
 */
public class SanPhamDAO {

    public static ArrayList<SanPhamPOJO> layDanhSachSanPham() {
        ArrayList<SanPhamPOJO> ds = new ArrayList<SanPhamPOJO>();

        String sql = "SELECT * FROM sanpham";
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                SanPhamPOJO sp = new SanPhamPOJO();
                sp.setMaSanPham(rs.getInt("maSanPham"));
                sp.setTenSanPham(rs.getString("tenSanPham"));
                sp.setMaLoaiSanPham(rs.getInt("maLoaiSanPham"));
                sp.setMaNhaSanXuat(rs.getInt("maNhaSanXuat"));
                sp.setGiaBan(rs.getFloat("giaBan"));
                sp.setSoLuong(rs.getInt("soLuong"));
                sp.setHinhAnh(rs.getString("hinhAnh"));
                sp.setThongSo(rs.getString("thongSo"));
                sp.setDangGiamGia(rs.getInt("dangGiamGia"));
                ds.add(sp);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        helper.close();
        return ds;
    }

    public static ArrayList<SanPhamPOJO> layDanhSachSanPhamTheoMaLoaiSanPham(int maloaisanpham) {
        ArrayList<SanPhamPOJO> ds = new ArrayList<SanPhamPOJO>();

        String sql = "SELECT * FROM sanpham where maloaisanpham = " + maloaisanpham;
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                SanPhamPOJO sp = new SanPhamPOJO();
                sp.setMaSanPham(rs.getInt("maSanPham"));
                sp.setTenSanPham(rs.getString("tenSanPham"));
                sp.setMaLoaiSanPham(rs.getInt("maLoaiSanPham"));
                sp.setMaNhaSanXuat(rs.getInt("maNhaSanXuat"));
                sp.setGiaBan(rs.getFloat("giaBan"));
                sp.setSoLuong(rs.getInt("soLuong"));
                sp.setHinhAnh(rs.getString("hinhAnh"));
                sp.setThongSo(rs.getString("thongSo"));
                sp.setDangGiamGia(rs.getInt("dangGiamGia"));
                ds.add(sp);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        helper.close();
        return ds;
    }

    public static ArrayList<SanPhamPOJO> layDanhSachSanPhamGiamGia() {
        ArrayList<SanPhamPOJO> ds = new ArrayList<SanPhamPOJO>();

        String sql = "SELECT * FROM sanpham where dangGiamGia = 1";
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                SanPhamPOJO sp = new SanPhamPOJO();
                sp.setMaSanPham(rs.getInt("maSanPham"));
                sp.setTenSanPham(rs.getString("tenSanPham"));
                sp.setMaLoaiSanPham(rs.getInt("maLoaiSanPham"));
                sp.setMaNhaSanXuat(rs.getInt("maNhaSanXuat"));
                sp.setGiaBan(rs.getFloat("giaBan"));
                sp.setSoLuong(rs.getInt("soLuong"));
                sp.setHinhAnh(rs.getString("hinhAnh"));
                sp.setThongSo(rs.getString("thongSo"));
                sp.setDangGiamGia(rs.getInt("dangGiamGia"));
                ds.add(sp);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        helper.close();
        return ds;
    }
    //layChiTietSanPham

    public static SanPhamPOJO layChiTietSanPham(int maSanPham) {
        SanPhamPOJO sp = new SanPhamPOJO();
        String sql = "SELECT * FROM sanpham where maSanPham = " + maSanPham;
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                sp.setMaSanPham(rs.getInt("maSanPham"));
                sp.setTenSanPham(rs.getString("tenSanPham"));
                sp.setMaLoaiSanPham(rs.getInt("maLoaiSanPham"));
                sp.setMaNhaSanXuat(rs.getInt("maNhaSanXuat"));
                sp.setGiaBan(rs.getFloat("giaBan"));
                sp.setSoLuong(rs.getInt("soLuong"));
                sp.setHinhAnh(rs.getString("hinhAnh"));
                sp.setThongSo(rs.getString("thongSo"));
                sp.setDangGiamGia(rs.getInt("dangGiamGia"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        helper.close();
        return sp;
    }
}